#include "TalkWindow.h"
#include "ui_TalkWindow.h"
#include "CommonUtils.h"
#include "RootcontactItem.h"
#include "ContactItem.h"
#include "CommonUtils.h"
#include "WindowManager.h"
#include <QToolTip>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include "SendFile.h"


//TalkWindow::TalkWindow(QWidget *parent, const QString str, GroupType type)                                                                                                                                                                            QString str, GroupType type)
//	: QWidget(parent)
//{
//	ui = new Ui::TalkWindow();
//	ui->setupUi(this);
//}


TalkWindow::TalkWindow(QWidget * parent, const QString uId)
	:QWidget(parent),
	ui(new Ui::TalkWindow()),
	m_talkId(uId)
{
	ui->setupUi(this);
	//ui->textEdit->setFontPointSize(10);

	setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);

	CommonUtils::loadStyleSheet(this, QString("TalkWindow"));

	WindowManager::getInstance()->addWindowId(m_talkId, this);

	initGroupTalk();
	initControl();

	//QFont font;
	////font.setPixelSize();
	//font.setPointSize(10);
	//ui->textEdit->setFont(font);

	//ui->textEdit->setFontPointSize(9);
	ui->textEdit->setFontPointSize(10);
}

TalkWindow::~TalkWindow()
{
	delete ui;
	WindowManager::getInstance()->deleteWindowId(m_talkId);
}

void TalkWindow::addEmotionImage(int value)
{
	ui->textEdit->setFocus();

	ui->textEdit->addEmotionUrl(value);

	ui->textEdit->setFontPointSize(10);
}

void TalkWindow::setWindowName(const QString & windowName)
{
	ui->nameLabel->setText(windowName);
}

void TalkWindow::setSignName(const QString & sign)
{
	ui->signLabel->setText(sign);
}

QString TalkWindow::getTalkId()
{
	return m_talkId;
}

void TalkWindow::apped(QString a, QString b)
{
	ui->msgWidget->appendMsg(a, b);
}

void TalkWindow::setID(QString a)
{
	ui->msgWidget->setID(a);
}

void TalkWindow::initGroupTalk()
{

	QString sql = QString(
		"SELECT * FROM tab_department WHERE departmentID=%1;"
	).arg(m_talkId);
	QSqlQueryModel sqModel;
	sqModel.setQuery(sql);

	int a = sqModel.rowCount();
	if (a > 0)
	{
		m_isGroupTalk = true;

	}
	else
	{
		m_isGroupTalk = false;
	}

}

void TalkWindow::onSetEmotionBtnStatus()
{
}

void TalkWindow::onItemDoubleClicked(QTreeWidgetItem * item, int c)
{
	bool k = item->data(0, Qt::UserRole).toBool();

	if (k)
	{
		WindowManager::getInstance()->addNewTalkWindow(
			item->data(0, Qt::UserRole + 1).toString()
		);

		//setWindowName();
		//const QString &name = 
		//	qobject_cast<ContactItem*>(
		//	ui->treeWidget->itemWidget(item, 0)
		//		)->getUserName();

		////setWindowName(name);
	}
}

void TalkWindow::initControl()
{
	ui->bodySplitter->setSizes(QList<int>{600, 138});

	//ui->textEdit->setFontPointSize(10);
	ui->textEdit->setFocus();

	connect(ui->sysmin, SIGNAL(clicked(bool)),
		parent(), SLOT(onShowMin(bool)));

	connect(ui->sysclose, SIGNAL(clicked(bool)),
		parent(), SLOT(onShowClose(bool)));
	connect(ui->closeBtn, SIGNAL(clicked(bool)),
		parent(), SLOT(onShowClose(bool)));

	connect(ui->faceBtn, SIGNAL(clicked(bool)),
		parent(), SLOT(onEmotionBtnChicked(bool)));

	connect(ui->sendBtn, SIGNAL(clicked(bool)),
		this, SLOT(onSendBtnChicked(bool)));

	connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
		this, SLOT(onItemDoubleClicked(QTreeWidgetItem*, int))
	);


	if (m_isGroupTalk)
	{
		init1Tilk();
	}
	else
	{
		init5Tilk();
	}
	//enum GroupType {
	//	COMPANY = 0,		//公司
	//	PERSONELGROUP,		//人事
	//	DEVELOPMENTGROUP,	//研发
	//	MARKETGROUP,		//市场
	//	PTOP,				//个人
	//	USER				//自己
	//};

	/*switch (m_groupType) {
	case COMPANY:
		init1Tilk();
		break;
	case PERSONELGROUP:
		init2Tilk();
		break;
	case DEVELOPMENTGROUP:
		init3Tilk();
		break;
	case MARKETGROUP:
		init4Tilk();
		break;
	case PTOP:
		init5Tilk();
		break;
	case USER:
		init16ilk();
		break;
	default:
		;
	}*/

	/*parent();
	__super;*/

}

void TalkWindow::init1Tilk()
{
	QTreeWidgetItem *pRootItem = new QTreeWidgetItem;
	pRootItem->setChildIndicatorPolicy(
		QTreeWidgetItem::ChildIndicatorPolicy::ShowIndicator);
	pRootItem->setData(0, Qt::UserRole, 0);

	QSqlQueryModel sqModel;
	if (m_talkId == "2000")
	{
		sqModel.setQuery("SELECT * FROM tab_employees WHERE status=1;");

	}
	else
	{
		sqModel.setQuery(
			QString("SELECT * FROM tab_employees WHERE status=1 AND departmentID=%1;")
			.arg(m_talkId)
		);
	}
	//QString sql = QString(
	//	"SELECT * FROM tab_employees WHERE departmentID=%1;"
	//).arg(m_talkId);
	//QSqlQueryModel sqModel;
	//sqModel.setQuery(sql);

	int a = sqModel.rowCount();
	//if (m_talkId == "2001")
	//{
	//	QString sql = QString(
	//		"SELECT * FROM tab_employees ;"
	//	);
	//	QSqlQueryModel sqModel;
	//	sqModel.setQuery(sql);

	//	a= sqModel.rowCount();
	//}

	int b = 0;
	////QString name=sqModel.data(sqModel.index())
	////QList<int> b;
	////b << sqModel.da
	////sqModel.index()


	RootcontactItem *pItemName = new RootcontactItem(false, ui->treeWidget);
	

	ui->treeWidget->addTopLevelItem(pRootItem);
	ui->treeWidget->setItemWidget(pRootItem, 0, pItemName);

	pRootItem->setExpanded(true);

	for (int i = 0; i < a; ++i)
	{	
		int online = sqModel.data(sqModel.index(i, 6)).toInt();
		if (online == 2)
		{
			b++;
		}

		initInfo(pRootItem, sqModel.data(sqModel.index(i, 0)).toInt());
	}

	//ui->signLabel->setText("一一一一");
	QString groupName;
	QSqlQuery query(
		QString("SELECT department_name from tab_department WHERE departmentID=%1;")
		.arg(m_talkId)
	);
	query.exec();
	if (query.next())
	{
		groupName = query.value(0).toString();
	}
	
	//, groupName, b, a
	pItemName->setText(
		QString("%1 %2 %3")
		.arg(groupName).arg(b).arg(a)
	);
}

void TalkWindow::init2Tilk()
{
	QTreeWidgetItem *pRootItem = new QTreeWidgetItem;
	pRootItem->setChildIndicatorPolicy(
		QTreeWidgetItem::ChildIndicatorPolicy::ShowIndicator);
	pRootItem->setData(0, Qt::UserRole, 0);

	RootcontactItem *pItemName = new RootcontactItem(false, ui->treeWidget);
	pItemName->setText(QString::asprintf("二二 %d %d", 0, 50));

	ui->treeWidget->addTopLevelItem(pRootItem);
	ui->treeWidget->setItemWidget(pRootItem, 0, pItemName);

	pRootItem->setExpanded(true);

	for (int i = 0; i < 15; ++i)
	{
		initInfo(pRootItem,0);
	}
}

void TalkWindow::init3Tilk()
{
	QTreeWidgetItem *pRootItem = new QTreeWidgetItem;
	pRootItem->setChildIndicatorPolicy(
		QTreeWidgetItem::ChildIndicatorPolicy::ShowIndicator);
	pRootItem->setData(0, Qt::UserRole, 0);

	RootcontactItem *pItemName = new RootcontactItem(false, ui->treeWidget);
	pItemName->setText(QString::asprintf("三三 %d %d", 0, 50));

	ui->treeWidget->addTopLevelItem(pRootItem);
	ui->treeWidget->setItemWidget(pRootItem, 0, pItemName);

	pRootItem->setExpanded(true);

	for (int i = 0; i < 8; ++i)
	{
		initInfo(pRootItem,0);
	}
}

void TalkWindow::init4Tilk()
{
	QTreeWidgetItem *pRootItem = new QTreeWidgetItem;
	pRootItem->setChildIndicatorPolicy(
		QTreeWidgetItem::ChildIndicatorPolicy::ShowIndicator);
	pRootItem->setData(0, Qt::UserRole, 0);

	RootcontactItem *pItemName = new RootcontactItem(false, ui->treeWidget);
	pItemName->setText(QString::asprintf("四四 %d %d", 0, 50));

	ui->treeWidget->addTopLevelItem(pRootItem);
	ui->treeWidget->setItemWidget(pRootItem, 0, pItemName);

	pRootItem->setExpanded(true);

	for (int i = 0; i < 12; ++i)
	{
		initInfo(pRootItem,0);
	}
}

void TalkWindow::init5Tilk()
{	
	QSqlQuery query(
		QString("SELECT * FROM tab_employees WHERE employeeID=%1;")
		.arg(m_talkId)
	);
	query.exec();
	query.next();

	QPixmap pixmap(":/Resources/MainWindow/skin.png");

	QLabel *label = new QLabel(ui->widget);
	label->setPixmap(pixmap);
	label->setSizePolicy(
		QSizePolicy::Policy::Expanding,
		QSizePolicy::Policy::Expanding
	);
	ui->verticalLayout_5->addWidget(label);
}

void TalkWindow::init16ilk()
{
}

void TalkWindow::initInfo(QTreeWidgetItem *rootItem ,int Id)
{
	QTreeWidgetItem *pChild = new QTreeWidgetItem;
	pChild->setData(0, Qt::UserRole, 1);
	pChild->setData(0, Qt::UserRole + 1, QString::number(Id));

	//RootcontactItem pItem = new RootcontactItem(false, ui->treeWidget);

	ContactItem *item = new ContactItem(ui->treeWidget);

	QSqlQuery query(
		QString("SELECT * FROM tab_employees WHERE employeeID=%1;")
		.arg(Id)
	);
	query.exec();
	if (query.next())
	{
		QPixmap pixmap =
			CommonUtils::getRoundImage(
				QPixmap(query.value(5).toString()),
				QPixmap(":/Resources/MainWindow/head_mask.png"),
				item->getHeadLabelSize()
			);

		item->setHeadPixmap(pixmap);

		rootItem->addChild(pChild);
		ui->treeWidget->setItemWidget(pChild, 0, item);

		//static int a = 0;
		item->setUserName(query.value(1).toString());
		item->setSignName(query.value(2).toString());
	}



}

void TalkWindow::onSendBtnChicked(bool)
{
	bool k;
	k = ui->textEdit->toPlainText().isEmpty();

	if (!k)
	{	
		
		const QString &html = ui->textEdit->toHtml();
		//QString s = ui->textEdit->toPlainText();
		ui->msgWidget->appendMsg(html);

		ui->textEdit->clear();
		//ui->textEdit->setPlainText("");
		ui->textEdit->delentAllEmotionImage();

		//ui->textEdit->setFontPointSize(10);
	}
	else
	{
		QToolTip::showText(
			mapToGlobal(QPoint(630, 660)),
			"消息为空",
			this,
			QRect(0, 0, 120, 100),
			3000
		);

		return;
	}

}

void TalkWindow::on_fileopenBtn_clicked()
{
	/*SendFile *send = new SendFile;
	send->exec();*/

	SendFile send;
	send.exec();
}
